﻿@namespace MudBlazor.Docs.Examples

<MudStack>
    <MudPaper Width="400px" MaxHeight="400px" Class="overflow-y-auto" Elevation="2">
        <MudTreeView Items="@TreeItems" ReadOnly>
            <ItemTemplate>
                @{                
                    var product = context.Value; // for convenient usage in the template
                }
                <MudTreeViewItem @bind-Expanded="@context.Expanded" Items="@context.Children" Value="@context.Value">
                    <Content>
                        <MudTreeViewItemToggleButton @bind-Expanded="@context.Expanded" Visible="@context.HasChildren" />
                        @if (product?.Price is not null) {
                            <MudCheckBox T="bool" Size="Size.Small" Value="@context.Selected" ValueChanged="@((v)=> OnCheckboxChanged(v, context))" />
                        }
                        <MudIcon Icon="@product?.Icon" Class="ml-0 mr-2" Color="@Color.Default" />
                        <MudText>@product?.Name</MudText>
                        @if (product?.Price is not null) {
                            <MudChip Class="ml-1">@product.Price.Value.ToString("C")</MudChip>
                        }
                    </Content>
                </MudTreeViewItem>
            </ItemTemplate>
        </MudTreeView>
    </MudPaper>
    <MudStack Class="mt-3" Style="width: 400px">
        <MudText Typo="@Typo.subtitle1" Inline>Selected items: @(string.Join(", ", (SelectedValues ?? []).Select(x => x.Name)))</MudText>
        <MudStack>
            <MudText Typo="@Typo.subtitle1">Sum: <MudChip T="string">@GetSelectedSum().ToString("C")</MudChip></MudText>
        </MudStack>
    </MudStack>
</MudStack>

@code {
    public void OnCheckboxChanged(bool selected, TreeItemData<Product> context)
    {
        context.Selected = selected;
        if (context.Value?.Price is null)
            return;
        if (context.Selected)
            SelectedValues.Add(context.Value);
        else
            SelectedValues.Remove(context.Value);
    }

    public HashSet<Product> SelectedValues { get; set; } = new();

    public List<TreeItemData<Product>> TreeItems { get; set; } = new();

    public class Product : IEquatable<Product>
    {
        public decimal? Price { get; set; }
        public string Name { get; init; }
        public string Icon { get; set; }

        public Product(string name, string icon, decimal? price = null)
        {
            Name = name;
            Icon = icon;
            Price = price;
        }

        public bool Equals(Product other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Name == other.Name;
        }

        public override bool Equals(object obj) => ReferenceEquals(this, obj) || obj is Product other && Equals(other);

        public override int GetHashCode() => Name?.GetHashCode() ?? 0;
    }

    protected override void OnInitialized()
    {
        TreeItems.Add(new TreeItemData<Product> { 
            Value=new Product("Coffee Makers", Icons.Material.Filled.Label), 
            Expanded = true,
            Children = [
                new TreeItemData<Product> { Value=new Product("Moka Pot", Icons.Material.Filled.LocalOffer, 36.99m),  },
                new TreeItemData<Product> { 
                    Value=new Product("French Press", Icons.Material.Filled.LocalOffer, 19.99m),
                    Expanded = true,
                    Children = [ 
                        new TreeItemData<Product> { Value = new Product("Spare Sieve", Icons.Material.Filled.LocalOffer, 6.00m) },
                        new TreeItemData<Product> { Value = new Product("Cleaning Kit", Icons.Material.Filled.LocalOffer, 17.59m) }
                    ]
                }
            ]
        });
        TreeItems.Add(new TreeItemData<Product> {
            Value = new Product("Tea Pots", Icons.Material.Filled.Label),
            Expanded = true,
            Children = [
                new TreeItemData<Product> {
                    Value=new Product("Glass Teapot", Icons.Material.Filled.LocalOffer, 36.99m),
                    Expanded = true,
                    Children = [ 
                        new TreeItemData<Product> { Value = new Product("Glass Infuser", Icons.Material.Filled.LocalOffer, 2.99m) }, 
                        new TreeItemData<Product> { Value = new Product("Stainless Steel Infuser", Icons.Material.Filled.LocalOffer, 5.99m) }
                    ]
                },
                new TreeItemData<Product> { Value = new Product("Stainless Steel Teapot", Icons.Material.Filled.LocalOffer, 14.15m) },
                new TreeItemData<Product> { Value = new Product("Japanese Cast Iron Teapot", Icons.Material.Filled.LocalOffer, 26.39m) },
                new TreeItemData<Product> { Value = new Product("Porcelain Teapot", Icons.Material.Filled.LocalOffer, 38.00m) }
            ]
        });
    }

    public decimal GetSelectedSum()
    {
        if (SelectedValues is null)
            return 0;
        return SelectedValues.Sum(p => p.Price ?? 0);
    }
}
